<?php

namespace App\Kuafu\Shop\Resources\B;

use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Service\WaiMaiShopService;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ShopSimpleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {

        return [
            'id'       => $this->id,
            'shopCode' => str_pad(
                $this->shopCode, ShopCodeEnum::CODE_TEXT[ShopCodeEnum::LEADING_ZERO],
                "0", STR_PAD_LEFT
            ),
            'name'     => $this->name,
            'bind'     => $this->getBindInfo(),
        ];
    }

    /**
     * @return array|array[]
     */
    public function getBindInfo(): array
    {
        $bind = [
            'meituanWaimai'  => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_MT_WM,
            ],
            'elemeWaimai'    => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_ELM,
            ],
            'meituanTuangou' => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_MT_TG,
            ],
            'douyin'         => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_DOUYIN,
            ],
        ];

        foreach ($this->waimaiShopMappings as $waimaiShopMapping) {
            switch ($waimaiShopMapping->sourceType) {
                case WaiMaiDBEnum::SOURCE_TYPE_MT_WM:
                    $bind['meituanWaimai']['bound']    = true;
                    $bind['meituanWaimai']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
                case WaiMaiDBEnum::SOURCE_TYPE_ELM:
                    $bind['elemeWaimai']['bound']    = true;
                    $bind['elemeWaimai']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
                case WaiMaiDBEnum::SOURCE_TYPE_MT_TG:
                    $bind['meituanTuangou']['bound']    = true;
                    $bind['meituanTuangou']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
                case WaiMaiDBEnum::SOURCE_TYPE_DOUYIN:
                    $bind['douyin']['bound']    = true;
                    $bind['douyin']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
            }
        }

        return $bind;
    }
}
